package com.example.myapplication;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.example.myapplication.http.RequestHttpClient;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle("登录");

        }
        //需要在监控点击事件的前面
        setContentView(R.layout.activity_main);

        //登录点击事件
        Button loginBtn = (Button) findViewById(R.id.login_btn);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                HashMap<String, String> map = new HashMap<String, String>();
                map.put("a", "1");
                RequestHttpClient $client = RequestHttpClient.getInstance(getApplicationContext());
                System.out.println("DDD");
                $client.post("http://127.0.0.1:8000",
                        map, new RequestHttpClient.MyCallback() {
                            @Override
                            public void success(Response res) throws IOException {
                                System.out.println(res);
                            }

                            @Override
                            public void failed(IOException e) {
                                System.out.println(e);
                            }
                        });

                Intent page = new Intent(MainActivity.this, UserActivity.class);
                startActivity(page);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}